#include "kepopuptoast.h"


// #include "../../uitools/vhc/ConstVarView.h"

#include <QTimer>
#include <QFont>
#include <QRectF>

#include "css4kepop.h"


KePopupToast::KePopupToast(QWidget *parent,const QString &text)
    : QWidget{parent}
{
    setAttribute(Qt::WA_TranslucentBackground);
    setWindowFlags(Qt::Popup);//windowFlags() | Qt::FramelessWindowHint |

    setStyleSheet(DefaultQSS_KeLabel);

    //背景是透明了，但是黑色,出现位置border-radius:6px
    // setAutoFillBackground(false);
    // setWindowOpacity(1.0);
    // setAttribute(Qt::WA_TranslucentBackground);

    auto layout = new QHBoxLayout(this);

    layout->setDirection(QBoxLayout::TopToBottom);
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setAlignment(Qt::AlignmentFlag::AlignLeft|Qt::AlignmentFlag::AlignTop);

    QLabel*   _label = new QLabel(text,this);
    // _label->setObjectName("specialLabel");
    // _label->setAttribute(Qt::WA_TranslucentBackground,true);
    // 没有效果  border-radius:6px;
    // _label->setStyleSheet("QLabel#specialLabel{color:#202020;background:#e8e8f6;border:1px solid #bbb;font-size:15px;padding:6px 8px;font-weight:bold;}");
    // _label->setStyleSheets("background-color:rgb(0,0,255);color:rgb(255,0,0);");
    // _label->setAlignment(Qt::AlignCenter);
    // _label->setAttribute(Qt::WA_TransparentForMouseEvents);//禁用向小部件及其子部件传递鼠标事件
    _label->adjustSize();
    // _label->setWordWrap(true);// 自动换行设置
    //设置后按钮虽然没有变灰，但是不会接受鼠标事件了
    // font.setPointSize(15);
    // _label->setFont(font);

    layout->addWidget(_label);

    setLayout(layout);



    // setStyleSheet("{border-radius:6px;}");
    /*
    int nLabelWidth,   nLabelHeight;
    QFont font;
    font.setPointSize(15);
    QFontMetrics fontMetrics(font);
    QRect rect = fontMetrics.boundingRect(text);
    nLabelWidth = 8+ rect.width();
    nLabelHeight = 8+ rect.height();
    QSizePolicy sizePoFixed(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed);
    sizePoFixed.setHorizontalStretch(0);
    sizePoFixed.setVerticalStretch(0);
    sizePoFixed.setHeightForWidth(false);

    setSizePolicy(sizePoFixed);
    setFixedSize(nLabelWidth,   nLabelHeight);
*/

}

KePopupToast::~KePopupToast()
{
    // qDebug()<<__FILE__<<__LINE__<<"~KePopupToast";
}

void KePopupToast::showAt(int msec,const QPoint &point)
{
    show();
    move(point);
    // QTimer::singleShot(msec,this,SLOT(timeoutClose()));
    QTimer::singleShot(msec,this,&KePopupToast::timeoutClose);
}

void KePopupToast::mousePressEvent(QMouseEvent *)
{
    // setAttribute(Qt::WA_NoMouseReplay);
    // setVisible(false);
    close();

}
/*
void KePopupToast::paintEvent(QPaintEvent *)
{
    qreal radius = 8;

    int widthWindow ,heightWidow;
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing |QPainter::VerticalSubpixelPositioning);

    QSize rectWin = this->size();
    widthWindow  = rectWin.width();
    heightWidow = rectWin.height();

    painter.fillRect(0,0,widthWindow ,heightWidow,Qt::transparent);
    QRectF rrc(0,0,widthWindow ,heightWidow);

    painter.setPen(Qt::NoPen);
    QBrush brushFill(QColor(QRgba64::fromArgb32(Default_Background)),Qt::SolidPattern);

    painter.setBrush(brushFill);

    painter.drawRoundedRect(rrc,radius,radius);

    QBrush brushText(QColor(QRgba64::fromArgb32(0xff5e5e5e)),Qt::SolidPattern);
    QPen penText(brushText,1.0f, Qt::SolidLine);

    painter.setPen(penText);
    painter.drawText(rrc,label);


}*/

void KePopupToast::timeoutClose()
{
    deleteLater();
}
